package com.vecspace.dayu.code.httphandler;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import com.vecspace.data.model.NameItem;
import com.vecspace.dayu.code.consts.TerminalType;
import com.vecspace.dayu.code.model.FuncVO;
import com.vecspace.dayu.code.pojo.DataTable;
import com.vecspace.dayu.code.pojo.Func;
import com.vecspace.dayu.code.service.DataTableService;
import com.vecspace.dayu.code.service.FuncService;
import com.vecspace.jsmodel.ZTreeNode;
import com.vecspace.security.model.LoginContext;
import com.vecspace.web.servlet.model.BaseController;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;

/** DAYU-功能定义 httphandler
 * 在handler中进行与页面相关的处理，比如把数据转换为HTML格式等。
 * @author wanghua
*/
@HController("dayucode_funcHandler")
public class FuncHandler extends BaseController {

	/**根据ID查询 DAYU-功能定义
	 * @return Func
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("getFunc")
	public Func getFunc(@Param("id") Long id) throws Exception {
		return funcService.getFunc(id);
	}
	
	@Path("getFuncVO")
	public FuncVO getFuncVO(@Param("id") Long id, @Param("projectId") Long projectId) throws Exception {
		Func func = null;
		if(id != null && id != 0) {
			func = funcService.getFunc(id);
			projectId = func.getProjectId();
		}
		List<NameItem> webPageLayouts = funcService.getValidPageLayouts(TerminalType.WEB);
		List<NameItem> appPageLayouts = funcService.getValidPageLayouts(TerminalType.APP);
		List<NameItem> padPageLayouts = funcService.getValidPageLayouts(TerminalType.PAD);
		List<String> javaPkgs = funcService.getJavaPkgs(projectId);
		List<DataTable> tables = dataTableService.getTableNames(projectId);
		
		FuncVO funcVO = new FuncVO(func);
		funcVO.setWebPageLayouts(webPageLayouts);
		funcVO.setAppPageLayouts(appPageLayouts);
		funcVO.setPadPageLayouts(padPageLayouts);
		funcVO.setJavaPkgs(javaPkgs);
		funcVO.setTables(tables);
		
		return funcVO;
	}
	
	/**逐级获取功能列表
	 * 
	 * @param projectId
	 * @param parentFuncId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	@Path("getChildFuncs")
	public List<ZTreeNode> getChildFuncNodes(@Param("projectId")Long projectId, @Param("pId")Long parentFuncId) throws Exception{
		List<Func> funcs = funcService.getChildFuncs(projectId, parentFuncId);
		
		List<ZTreeNode> nodes = funcs.stream().map(func->{
			ZTreeNode node = new ZTreeNode(func.getId().toString(), func.getParentId().toString(), func.getName(), "", func.getOrdnum());
			node.setIsParent(true);
			return node;
		}).collect(Collectors.toList());
		
		return nodes;
	}
	
	/**获取系统的所有功能
	 * 
	 * @param projectId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	@Path("getAllFuncs")
	public List<Func> getAllFuncs(@Param("projectId")Long projectId) throws Exception {
		List<Func> funcs = funcService.getAllFuncs(projectId);
		return funcs;
	}
	
	/** 插入或修改 DAYU-功能定义 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("saveFunc")
	public Func saveFunc(@Param("item") Func item) throws Exception {
		if (item == null){
			return null;
		}
		/*** 对item对象进行校验，如必填项，格式，逻辑关系等 ***/
		String loginUserId = LoginContext.getLoginId();
		return funcService.saveOrCreateFunc(item, loginUserId);
	}

	/** 根据ID删除 DAYU-功能定义 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("deleteFunc")
	public void deleteFunc(@Param("id") Long id) throws Exception {
		funcService.deleteFunc(id);
	}

	/************* property ********************/
	@Autowired
	private FuncService funcService;
	
	@Autowired
	private DataTableService dataTableService;

}
